package com.letv.whatslive.model.convert;

import com.letv.whatslive.model.CP;
import com.letv.whatslive.model.GlobalResource;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.StringUtils;

//2015.11.4

/**
 * Created by shenyue on 2015/11/4.
 */
public class GlobalResourceConvert {
    public static DBObject castGlobalResourceToDBObject(GlobalResource globalResource){
        DBObject dbo = new BasicDBObject();
        if(globalResource.getId()!=null){
            dbo.put("_id",globalResource.getId());
        }

        if(globalResource.getResourceType()!=null){
            dbo.put("resourceType",globalResource.getResourceType());
        }else{
            dbo.put("resourceType",0);
        }
        if(StringUtils.isNotBlank(globalResource.getResourceDescribe())){
            dbo.put("resourceDescribe",globalResource.getResourceDescribe());
        }else{
            dbo.put("resourceDescribe","");
        }
        if(StringUtils.isNotBlank(globalResource.getResourceUrl())){
            dbo.put("resourceUrl",globalResource.getResourceUrl());
        }else{
            dbo.put("resourceUrl","");
        }
        if(StringUtils.isNotBlank(globalResource.getResourceMd5())){
            dbo.put("resourceMd5",globalResource.getResourceMd5());
        }else{
            dbo.put("resourceMd5","");
        }


        return dbo;


//        DBObject dbo = new BasicDBObject();
//        if (device.getId() != null) {
//            dbo.put("_id", device.getId());
//        }
//        dbo.put("platformId",device.getPlatformId());
//        dbo.put("sysVer", device.getSysVer());
//        dbo.put("model", device.getModel());
//        dbo.put("appVer", device.getAppVer());
//        dbo.put("corporationId", device.getCorporationId());
//        dbo.put("channelId", device.getChannelId());
//        return dbo;
    }

    public static GlobalResource castDBObjectToGlobalResource(DBObject dbObject){
        GlobalResource globalResource = new GlobalResource();
        if(dbObject != null) {
            globalResource.setId(ObjUtils.toLong(dbObject.get("_id")));
            globalResource.setResourceDescribe(ObjUtils.toString(dbObject.get("resourceDescribe")));
            globalResource.setResourceType(ObjUtils.toInteger(dbObject.get("resourceType")));
            globalResource.setResourceUrl(ObjUtils.toString(dbObject.get("resourceUrl")));
            globalResource.setResourceMd5(ObjUtils.toString(dbObject.get("resourceMd5")));
        }

        return globalResource;
    }
}
